package com.yami.shop.consign.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.vo.MarketAnalysisVO;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.consign.common.model.ConsignAnalysis;
import com.yami.shop.consign.common.model.ConsignOptional;
import com.yami.shop.consign.common.service.ConsignAnalysisService;
import com.yami.shop.consign.common.service.ConsignOptionalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/ua/consign/analysis")
@Tag(name = "寄售市场分析（c端）")
@Slf4j
@AllArgsConstructor
public class ConsignAnalysisController {
    
    private final ConsignAnalysisService consignAnalysisService;
    
    private final ConsignOptionalService consignOptionalService;
    
    @GetMapping("/page")
    @Operation(summary =  "市场分析分页列表", description = "市场分析分页列表")
    public ServerResponseEntity<PageVO<MarketAnalysisVO>> page(@Valid PageDTO pageDTO, Integer sort, String userId, String prodName){
        PageVO<MarketAnalysisVO> page = consignAnalysisService.getPage(pageDTO, sort, 1, userId, prodName);
        return ServerResponseEntity.success(page);
    }
    
    @GetMapping("/upsDowns")
    @Operation(summary =  "涨跌数量统计", description = "涨跌数量统计")
    public ServerResponseEntity<Map<String, Integer>> getUpsDowns(){
        Map<String, Integer> map = consignAnalysisService.upsDowns(null);
        return ServerResponseEntity.success(map);
    }
    
    @GetMapping("/info")
    @Operation(summary =  "市场分析详情", description = "市场分析详情")
    public ServerResponseEntity<MarketAnalysisVO> getById(@RequestParam Long id, Long userId){
        ConsignAnalysis analysis = consignAnalysisService.getById(id);
        MarketAnalysisVO analysisVO = BeanUtil.copyProperties(analysis, MarketAnalysisVO.class);
        if (Objects.nonNull(userId)) {
            ConsignOptional one = consignOptionalService.getOne(new LambdaQueryWrapper<ConsignOptional>().eq(ConsignOptional::getProdId, analysis.getProdId()).eq(ConsignOptional::getUserId, userId));
            if (Objects.nonNull(one)) {
                analysisVO.setIsOptional(true);
            }
        }
        return ServerResponseEntity.success(analysisVO);
    }
    
    
    
    
    
    
}
